<%@ page import="com.walnut.pojo.Food" %>
<%@ page import="java.util.List" %>
<%@ page import="com.walnut.pojo.Order" %>
<%@ page import="jakarta.servlet.ServletContext" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>订单列表</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            margin: 20px;
            background-color: #f9f9f9;
        }
        .container {
            width: 100%;
            max-width: 1200px;
            margin: 0 auto;
        }
        table {
            width: 100%;
            border-collapse: collapse;
            margin-top: 20px;
        }
        th, td {
            border: 1px solid #ccc;
            padding: 10px;
            text-align: center;
        }
        th {
            background-color: #f0f0f0;
            font-weight: bold;
        }
        tr:nth-child(even) {
            background-color: #f9f9f9;
        }
        tr:hover {
            background-color: #e0e0e0;
        }
        img {
            max-width: 100px;
            height: auto;
            border-radius: 5px;
        }
        .actions a {
            display: inline-block;
            padding: 8px 15px;
            margin: 0 5px;
            color: #FFA500;
            text-decoration: none;
            font-size: 14px;
            cursor: pointer;
            transition: color 0.3s;
        }
        .actions a:hover {
            color: #FF7F00;
        }
        h1 {
            text-align: left;
            margin-bottom: 20px;
        }
        .base-button{
            display: inline-block;
            padding: 10px 20px;
            border: 1px solid #ccc;
            border-radius: 5px;
            background-color: #d3d3d3;
            color: black;
            font-size: 16px;
            cursor: pointer;
            text-decoration: none;
            transition: background-color 0.3s, color 0.3s;
        }
        .base-button:hover{
            background-color: #c0c0c0;
        }

        .logout-button{
            display: inline-block;
            padding: 10px 20px;
            border: 1px solid #ccc;
            border-radius: 5px;
            background-color: #d3d3d3;
            color: black;
            font-size: 16px;
            cursor: pointer;
            text-decoration: none;
            transition: background-color 0.3s, color 0.3s;
            float: right;
        }
        .logout-button:hover{
            background-color: #c0c0c0;
        }

        /* 模态框背景 */
        .modal {
            display: none; /* 默认隐藏 */
            position: fixed; /* 固定定位 */
            z-index: 1; /* 置于顶层 */
            left: 0;
            top: 0;
            width: 100%; /* 全屏宽度 */
            height: 100%; /* 全屏高度 */
            overflow: auto; /* 溢出滚动 */
            background-color: rgba(0,0,0,0.4); /* 半透明背景 */
        }

        /* 模态框内容 */
        .modal-content {
            background-color: #fefefe;
            margin: 15% auto; /* 距离顶部15%，水平居中 */
            padding: 20px;
            border: 1px solid #888;
            width: 30%; /* 宽度30% */
            max-width: 400px; /* 最大宽度400px */
            text-align: center;
        }

        /* 关闭按钮 */
        .close {
            color: #aaa;
            float: right;
            font-size: 28px;
            font-weight: bold;
        }

        .close:hover,
        .close:focus {
            color: black;
            text-decoration: none;
            cursor: pointer;
        }

        /* 模态框按钮 */
        .modal-buttons button {
            padding: 10px 20px;
            margin: 10px;
            border: none;
            border-radius: 5px;
            cursor: pointer;
            font-size: 16px;
        }

        #confirmDeleteBtn {
            background-color: #dc3545;
            color: white;
        }

        #confirmDeleteBtn:hover {
            background-color: #c82333;
        }

        #cancelDeleteBtn {
            background-color: #6c757d;
            color: white;
        }

        #cancelDeleteBtn:hover {
            background-color: #5a6268;
        }

        /* 查询容器，居中对齐 */
        .search-container {
            display: flex;
            justify-content: center; /* 水平居中 */
            align-items: center;     /* 垂直居中 */
            gap: 15px;
            margin: 20px 0;
        }

        /* 查询输入框的统一样式 */
        .search-container input, .search-container select {
            padding: 10px;
            font-size: 14px;
            border: 1px solid #ccc;
            border-radius: 5px;
            background-color: #fff;
            color: #333;
            width: 250px;  /* 设置宽度为250px，保证布局合理 */
            box-sizing: border-box;
            transition: all 0.3s ease; /* 平滑过渡效果 */
        }

        /* 输入框聚焦时的效果 */
        .search-container input:focus, .search-container select:focus {
            border-color: #007BFF;  /* 聚焦时修改边框颜色 */
            outline: none;  /* 去除默认的outline */
            box-shadow: 0 0 5px rgba(0, 123, 255, 0.3); /* 聚焦时添加阴影 */
        }

        /* 查询按钮样式 */
        .search-container button {
            background-color: #007BFF;
            color: white;
            border: none;
            padding: 10px 20px;
            cursor: pointer;
            border-radius: 5px;
            transition: background-color 0.3s ease;
            font-size: 14px;
        }

        .search-container button:hover {
            background-color: #0056b3;
        }

        /* 响应式布局：调整小屏幕时的输入框布局 */
        @media (max-width: 768px) {
            .search-container {
                flex-direction: column;
                align-items: stretch;
            }

            .search-container input, .search-container select, .search-container button {
                width: 100%;  /* 在小屏幕下宽度为100% */
                margin-bottom: 10px;
            }
        }

    </style>
</head>
<body>

<div class="search-container">
    <input type="text" id="searchGoodsName" placeholder="请输入商品名称..." />
    <input type="text" id="searchPrice" placeholder="请输入商品价格..." />
    <select id="searchGoodsType">
        <option value="">无</option>
        <option value="食品">食品</option>
        <option value="日用品">日用品</option>、
        <option value="服装">服装</option>
    </select>
    <button class="base-button" onclick="searchOrder()">查询</button>
</div>

<div class="container">
    <h1>订单列表</h1>

    <!-- 返回按钮 -->
    <a class="base-button" href="adminFoodList.jsp">返回</a>

    <!-- 退出登录按钮 -->
    <a class="logout-button" onclick="confirmLogout()">退出登录</a>
    <!-- 表格头部 -->
    <table>
        <thead>
        <tr>
            <th>序号</th>
            <th>货物名称</th>
            <th>价格</th>
            <th>货物种类</th>
            <th>购买人</th>
            <th>下单时间</th>
            <th>操作</th>
        </tr>
        </thead>
        <tbody>
        <%
            // 获取订单列表
            ServletContext context = request.getServletContext();
            List<Order> orders = (List<Order>) context.getAttribute("orders");
            if (orders != null && !orders.isEmpty()) {
                int index = 1; // 序号从1开始
                for (Order order : orders) {
        %>
        <tr>
        <tr>
            <td><%= index %></td>
            <td><%= order.getGoodsName() %></td>
            <td><%= order.getPrice() %></td>
            <td><%= order.getGoodsType() %></td>
            <td><%= order.getPurchaser() %></td>
            <td><%= order.getCreateTime() %></td>
            <td class="actions">
                <a href="javascript:void(0);" class="delete" onclick="confirmDelete('<%= order.getGoodsId() %>')">删除</a>
            </td>
        </tr>
        </tr>
        <%
                index++; // 序号递增
            }
        } else {
        %>
        <tr>
            <td colspan="6">没有找到相关订单。</td> <!-- 如果没有订单 -->
        </tr>
        <%
            }
        %>
        </tbody>
    </table>
</div>

<!-- 模态框 -->
<div id="deleteModal" class="modal">
    <div class="modal-content">
        <span class="close">&times;</span>
        <p>确定要删除此订单吗？</p>
        <div class="modal-buttons">
            <button id="confirmDeleteBtn">确认</button>
            <button id="cancelDeleteBtn">取消</button>
        </div>
    </div>
</div>

<!-- 退出登录模态框 -->
<div id="logoutModal" class="modal">
    <div class="modal-content">
        <span class="close">&times;</span>
        <p>是否退出登录？</p>
        <div class="modal-buttons">
            <button id="confirmLogoutBtn">确认</button>
            <button id="cancelLogoutBtn">取消</button>
        </div>
    </div>
</div>

<script>
    var deleteModal = document.getElementById("deleteModal");
    var span = document.getElementsByClassName("close")[0];
    var confirmDeleteBtn = document.getElementById("confirmDeleteBtn");
    var cancelDeleteBtn = document.getElementById("cancelDeleteBtn");

    var logoutModal = document.getElementById("logoutModal");
    var logoutSpan = document.getElementsByClassName("close")[1];
    var confirmLogoutBtn = document.getElementById("confirmLogoutBtn");
    var cancelLogoutBtn = document.getElementById("cancelLogoutBtn");

    // 打开模态框
    function confirmDelete(goodsId) {
        document.getElementById("confirmDeleteBtn").setAttribute("data-goods-id", goodsId);
        deleteModal.style.display = "block";
    }
    // 关闭模态框
    span.onclick = function() {
        deleteModal.style.display = "none";
    }
    // 点击确认按钮
    confirmDeleteBtn.onclick = function() {
        var goodsId = this.getAttribute("data-goods-id");
        // 使用 AJAX 发送异步请求
        var xhr = new XMLHttpRequest();
        xhr.open('GET', '/deleteOrder?goodsId=' + goodsId, true);
        // 关闭模态框
        deleteModal.style.display = "none";
        xhr.send();
        window.location.reload();
    }
    // 点击取消按钮
    cancelDeleteBtn.onclick = function() {
        deleteModal.style.display = "none";
    }
    // 点击模态框外部关闭
    window.onclick = function(event) {
        if (event.target == deleteModal) {
            deleteModal.style.display = "none";
        }
    }

    // 打开模态框
    function confirmLogout() {
        logoutModal.style.display = "block";
    }
    // 关闭模态框
    logoutSpan.onclick = function() {
        logoutModal.style.display = "none";
    }
    // 点击确认按钮
    confirmLogoutBtn.onclick = function() {
        // 发送请求到后端，通知服务器删除 loginer 字段
        var xhr = new XMLHttpRequest();
        xhr.open('GET', '/logout', true); // 假设你创建了一个 "/logout" 的后端请求
        xhr.onreadystatechange = function() {
            if (xhr.readyState === 4 && xhr.status === 200) {
                // 成功移除 loginer 后，跳转到 login.jsp
                window.location.href = "login.jsp";
                logoutModal.style.display = "none";
            }
        };
        xhr.send();
    }
    // 点击取消按钮
    cancelLogoutBtn.onclick = function() {
        logoutModal.style.display = "none";
    }
    // 点击模态框外部关闭
    window.onclick = function(event) {
        if (event.target == logoutModal) {
            logoutModal.style.display = "none";
        }
    }

    // 搜索功能
    function searchOrder() {
        var goodsName = document.getElementById("searchGoodsName").value.trim();
        var price = document.getElementById("searchPrice").value.trim();
        var goodsType = document.getElementById("searchGoodsType").value.trim();

        // 构建查询参数
        var queryParams = [];
        if (goodsName) queryParams.push("goodsName=" + encodeURIComponent(goodsName));
        if (price) queryParams.push("price=" + encodeURIComponent(price));
        if (goodsType) queryParams.push("goodsType=" + encodeURIComponent(goodsType));

        var queryString = queryParams.length > 0 ? "?" + queryParams.join("&") : "";

        // 使用 AJAX 请求后端进行查询
        var xhr = new XMLHttpRequest();
        xhr.open('GET', '/selectOrder' + queryString, true);
        xhr.onload = function() {
            if (xhr.status === 200) {
                // 直接插入返回的 HTML
                document.querySelector("table tbody").innerHTML = xhr.responseText;
            }
        };
        xhr.send();
    }
</script>
</body>
</html>